package edu.buffalo.cse.cse486_586.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

import android.util.Log;

public class CommonUtil {
	public static final String TAG = "CommonUtil";

	public static boolean isNumeric(String str) {
		return str.matches("-?\\d+(.\\d+)?");
	}

	public static String genHash(String input) {
		MessageDigest sha1 = null;
		try {
			sha1 = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			Log.d(TAG, "Exception", e);
		}
		byte[] sha1Hash = sha1.digest(input.getBytes());
		Formatter formatter = new Formatter();
		for (byte b : sha1Hash) {
			formatter.format("%02x", b);
		}
		return formatter.toString();
	}

	public static int getRedirectPort(int consolePort) {
		int index = (consolePort - AppUtil.MASTER_CONSOLE_PORT)
				/ AppUtil.STEP_PORT;
		return AppUtil.MASTER_REDIRCT_PORT + (index * AppUtil.STEP_REDIRCT);
	}
}
